<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
  <meta name="theme-color" content="#222">
  <meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

  <link rel="stylesheet" href="/css/main.css">



  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

  <script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {
      "hostname": "holidaypenguin.gitee.io",
      "root": "/",
      "images": "/images",
      "scheme": "Mist",
      "version": "8.2.2",
      "exturl": false,
      "sidebar": {
        "position": "right",
        "display": "always",
        "padding": 18,
        "offset": 12
      },
      "copycode": true,
      "bookmark": {
        "enable": false,
        "color": "#222",
        "save": "auto"
      },
      "fancybox": false,
      "mediumzoom": false,
      "lazyload": false,
      "pangu": false,
      "comments": {
        "style": "tabs",
        "active": null,
        "storage": true,
        "lazyload": false,
        "nav": null
      },
      "motion": {
        "enable": true,
        "async": true,
        "transition": {
          "post_block": "fadeIn",
          "post_header": "fadeInDown",
          "post_body": "fadeInDown",
          "coll_header": "fadeInLeft",
          "sidebar": "slideUpIn"
        }
      },
      "prism": false,
      "i18n": {
        "placeholder": "搜索...",
        "empty": "没有找到任何搜索结果：${query}",
        "hits_time": "找到 ${hits} 个搜索结果（用时 ${time} 毫秒）",
        "hits": "找到 ${hits} 个搜索结果"
      },
      "path": "/search.xml",
      "localsearch": {
        "enable": true,
        "trigger": "auto",
        "top_n_per_article": 1,
        "unescape": false,
        "preload": false
      }
    };
  </script>
  <meta property="og:type" content="article">
  <meta property="og:title" content="微信小程组件开发合集">
  <meta property="og:url" content="https://holidaypenguin.gitee.io/blob/2018-11-16-wechat-component-development-collection/index.html">
  <meta property="og:site_name" content="HolidayPenguin">
  <meta property="og:locale" content="zh_CN">
  <meta property="og:image" content="https://holidaypenguin.gitee.io/images/wechatapp/02.png">
  <meta property="og:image" content="https://holidaypenguin.gitee.io/images/wechatapp/03.png">
  <meta property="article:published_time" content="2018-11-16T03:28:22.000Z">
  <meta property="article:modified_time" content="2018-11-16T03:28:22.000Z">
  <meta property="article:author" content="holidaypenguin">
  <meta property="article:tag" content="微信小程序">
  <meta property="article:tag" content="小程序自定义组件">
  <meta name="twitter:card" content="summary">
  <meta name="twitter:image" content="https://holidaypenguin.gitee.io/images/wechatapp/02.png">


  <link rel="canonical" href="https://holidaypenguin.gitee.io/blob/2018-11-16-wechat-component-development-collection/">


  <script class="page-configurations">
    // https://hexo.io/docs/variables.html
    CONFIG.page = {
      sidebar: "",
      isHome: false,
      isPost: true,
      lang: 'zh-CN'
    };
  </script>
  <title>微信小程组件开发合集 | HolidayPenguin</title>





  <noscript>
    <style>
      body {
        margin-top: 2rem;
      }

      .use-motion .menu-item,
      .use-motion .sidebar,
      .use-motion .post-block,
      .use-motion .pagination,
      .use-motion .comments,
      .use-motion .post-header,
      .use-motion .post-body,
      .use-motion .collection-header {
        visibility: visible;
      }

      .use-motion .header,
      .use-motion .site-brand-container .toggle,
      .use-motion .footer {
        opacity: initial;
      }

      .use-motion .site-title,
      .use-motion .site-subtitle,
      .use-motion .custom-logo-image {
        opacity: initial;
        top: initial;
      }

      .use-motion .logo-line {
        transform: scaleX(1);
      }

      .search-pop-overlay,
      .sidebar-nav {
        display: none;
      }

      .sidebar-panel {
        display: block;
      }
    </style>
  </noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">
        <div class="site-brand-container">
          <div class="site-nav-toggle">
            <div class="toggle" aria-label="切换导航栏" role="button">
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
            </div>
          </div>

          <div class="site-meta">

            <a href="/" class="brand" rel="start">
              <i class="logo-line"></i>
              <h1 class="site-title">HolidayPenguin</h1>
              <i class="logo-line"></i>
            </a>
          </div>

          <div class="site-nav-right">
            <div class="toggle popup-trigger">
              <i class="fa fa-search fa-fw fa-lg"></i>
            </div>
          </div>
        </div>



        <nav class="site-nav">
          <ul class="main-menu menu">
            <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
            <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
            <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
            <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
            <li class="menu-item menu-item-search">
              <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
              </a>
            </li>
          </ul>
        </nav>



        <div class="search-pop-overlay">
          <div class="popup search-popup">
            <div class="search-header">
              <span class="search-icon">
                <i class="fa fa-search"></i>
              </span>
              <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
              </div>
              <span class="popup-btn-close" role="button">
                <i class="fa fa-times-circle"></i>
              </span>
            </div>
            <div class="search-result-container no-result">
              <div class="search-result-icon">
                <i class="fa fa-spinner fa-pulse fa-5x"></i>
              </div>
            </div>

          </div>
        </div>

      </div>


      <div class="toggle sidebar-toggle" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
      </div>

      <aside class="sidebar">

        <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
          <ul class="sidebar-nav">
            <li class="sidebar-nav-toc">
              文章目录
            </li>
            <li class="sidebar-nav-overview">
              站点概览
            </li>
          </ul>

          <div class="sidebar-panel-container">
            <!--noindex-->
            <div class="post-toc-wrap sidebar-panel">
              <div class="post-toc animated">
                <ol class="nav">
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E6%94%AF%E6%8C%81"><span class="nav-number">1.</span> <span class="nav-text">版本支持</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AF%BC%E8%AF%BB"><span class="nav-number">2.</span> <span class="nav-text">导读</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6%E5%8A%9F%E8%83%BD"><span class="nav-number">3.</span> <span class="nav-text">自定义组件功能</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E6%A8%A1%E6%9D%BF"><span class="nav-number">3.1.</span> <span class="nav-text">组件模板</span></a>
                        <ol class="nav-child">
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A"><span class="nav-number">3.1.1.</span> <span class="nav-text">模板数据绑定</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84slot"><span class="nav-number">3.1.2.</span> <span class="nav-text">组件的slot</span></a></li>
                        </ol>
                      </li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E6%A0%B7%E5%BC%8F"><span class="nav-number">3.2.</span> <span class="nav-text">组件样式</span></a>
                        <ol class="nav-child">
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8D%E6%94%AF%E6%8C%81%E7%9A%84%E5%86%99%E6%B3%95"><span class="nav-number">3.2.1.</span> <span class="nav-text">不支持的写法</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%96%E9%83%A8%E6%A0%B7%E5%BC%8F%E7%B1%BB"><span class="nav-number">3.2.2.</span> <span class="nav-text">外部样式类</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%A8%E5%B1%80%E6%A0%B7%E5%BC%8F%E7%B1%BB"><span class="nav-number">3.2.3.</span> <span class="nav-text">全局样式类</span></a></li>
                        </ol>
                      </li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E6%9E%84%E9%80%A0%E5%99%A8"><span class="nav-number">3.3.</span> <span class="nav-text">组件构造器</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E4%BA%8B%E4%BB%B6"><span class="nav-number">3.4.</span> <span class="nav-text">组件事件</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">3.5.</span> <span class="nav-text">组件生命周期</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#behaviors"><span class="nav-number">3.6.</span> <span class="nav-text">behaviors</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E9%97%B4%E5%85%B3%E7%B3%BB"><span class="nav-number">3.7.</span> <span class="nav-text">组件间关系</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8A%BD%E8%B1%A1%E8%8A%82%E7%82%B9"><span class="nav-number">3.8.</span> <span class="nav-text">抽象节点</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6%E6%89%A9%E5%B1%95"><span class="nav-number">3.9.</span> <span class="nav-text">自定义组件扩展</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6"><span class="nav-number">3.10.</span> <span class="nav-text">使用自定义组件</span></a>
                        <ol class="nav-child">
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E5%B1%80%E9%83%A8%E7%BB%84%E4%BB%B6"><span class="nav-number">3.10.1.</span> <span class="nav-text">声明局部组件</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E5%85%A8%E5%B1%80%E7%BB%84%E4%BB%B6"><span class="nav-number">3.10.2.</span> <span class="nav-text">声明全局组件</span></a></li>
                        </ol>
                      </li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%80%E5%8F%91%E7%AC%AC%E4%B8%89%E6%96%B9%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6"><span class="nav-number">4.</span> <span class="nav-text">开发第三方自定义组件</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%80%E5%8F%91"><span class="nav-number">4.1.</span> <span class="nav-text">开发</span></a>
                        <ol class="nav-child">
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-number">4.1.1.</span> <span class="nav-text">初始化</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%80%E5%8F%91%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="nav-number">4.1.2.</span> <span class="nav-text">开发注意事项</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%96%E5%86%99%E6%96%87%E6%A1%A3"><span class="nav-number">4.1.3.</span> <span class="nav-text">编写文档</span></a></li>
                        </ol>
                      </li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%91%E5%B8%83"><span class="nav-number">4.2.</span> <span class="nav-text">发布</span></a>
                        <ol class="nav-child">
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E5%B0%8F%E7%A8%8B%E5%BA%8F-npm-%E5%8C%85%E7%9A%84%E7%BA%A6%E6%9D%9F"><span class="nav-number">4.2.1.</span> <span class="nav-text">发布小程序 npm 包的约束</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E5%85%B6%E4%BB%96-npm-%E5%8C%85%E7%9A%84%E7%BA%A6%E6%9D%9F"><span class="nav-number">4.2.2.</span> <span class="nav-text">发布其他 npm 包的约束</span></a></li>
                          <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E6%B5%81%E7%A8%8B"><span class="nav-number">4.2.3.</span> <span class="nav-text">发布流程</span></a></li>
                        </ol>
                      </li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8"><span class="nav-number">4.3.</span> <span class="nav-text">使用</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%98%E6%96%B9%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6"><span class="nav-number">4.4.</span> <span class="nav-text">官方自定义组件</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%98%E6%96%B9%E6%89%A9%E5%B1%95"><span class="nav-number">4.5.</span> <span class="nav-text">官方扩展</span></a></li>
                    </ol>
                  </li>
                </ol>
              </div>
            </div>
            <!--/noindex-->

            <div class="site-overview-wrap sidebar-panel">
              <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
                <p class="site-author-name" itemprop="name">holidaypenguin</p>
                <div class="site-description" itemprop="description"></div>
              </div>
              <div class="site-state-wrap site-overview-item animated">
                <nav class="site-state">
                  <div class="site-state-item site-state-posts">
                    <a href="/archives/">

                      <span class="site-state-item-count">138</span>
                      <span class="site-state-item-name">日志</span>
                    </a>
                  </div>
                  <div class="site-state-item site-state-categories">
                    <a href="/categories/">

                      <span class="site-state-item-count">26</span>
                      <span class="site-state-item-name">分类</span></a>
                  </div>
                  <div class="site-state-item site-state-tags">
                    <a href="/tags/">

                      <span class="site-state-item-count">234</span>
                      <span class="site-state-item-name">标签</span></a>
                  </div>
                </nav>
              </div>
              <div class="links-of-author site-overview-item animated">
                <span class="links-of-author-item">
                  <a href="https://github.com/holidaypenguin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;holidaypenguin" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
                </span>
                <span class="links-of-author-item">
                  <a href="mailto:songshipeng2016@gmail.com" title="E-Mail → mailto:songshipeng2016@gmail.com" rel="noopener" target="_blank"><i class="envelope fa-fw"></i>E-Mail</a>
                </span>
              </div>



            </div>
          </div>
        </div>
      </aside>
      <div class="sidebar-dimmer"></div>


    </header>


    <div class="back-to-top" role="button">
      <i class="fa fa-arrow-up"></i>
      <span>0%</span>
    </div>

    <noscript>
      <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
    </noscript>


    <div class="main-inner post posts-expand">





      <div class="post-block">



        <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
          <link itemprop="mainEntityOfPage" href="https://holidaypenguin.gitee.io/blob/2018-11-16-wechat-component-development-collection/">

          <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
            <meta itemprop="image" content="/images/avatar.gif">
            <meta itemprop="name" content="holidaypenguin">
            <meta itemprop="description" content="">
          </span>

          <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
            <meta itemprop="name" content="HolidayPenguin">
          </span>
          <header class="post-header">
            <h1 class="post-title" itemprop="name headline">
              微信小程组件开发合集
            </h1>

            <div class="post-meta-container">
              <div class="post-meta">
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-calendar"></i>
                  </span>
                  <span class="post-meta-item-text">发表于</span>

                  <time title="创建时间：2018-11-16 11:28:22" itemprop="dateCreated datePublished" datetime="2018-11-16T11:28:22+08:00">2018-11-16</time>
                </span>
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-folder"></i>
                  </span>
                  <span class="post-meta-item-text">分类于</span>
                  <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                    <a href="/categories/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" itemprop="url" rel="index"><span itemprop="name">微信小程序</span></a>
                  </span>
                </span>


                <span id="/blob/2018-11-16-wechat-component-development-collection/" class="post-meta-item leancloud_visitors" data-flag-title="微信小程组件开发合集" title="阅读次数">
                  <span class="post-meta-item-icon">
                    <i class="far fa-eye"></i>
                  </span>
                  <span class="post-meta-item-text">阅读次数：</span>
                  <span class="leancloud-visitors-count"></span>
                </span>
              </div>

              <div class="post-description">
                <!-- more -->
              </div>
            </div>
          </header>




          <div class="post-body" itemprop="articleBody">
            <h1 id="版本支持"><a href="#版本支持" class="headerlink" title="版本支持"></a>版本支持</h1>
            <ul>
              <li>从小程序基础库版本 1.6.3 开始支持简洁的组件化编程</li>
              <li>开发者工具 1.02.1810190 及以上版本支持全局组件</li>
              <li>自基础库版本 2.0.9 开始，还可以在组件绑定数据中包含函数</li>
              <li><code>:host</code> 选择器需要包含基础库 1.7.2 或更高版本的开发者工具支持</li>
              <li>外部样式类这个特性从小程序基础库版本 1.9.90 开始支持</li>
              <li>全局样式类这个特性从小程序基础库版本 2.2.3 开始支持</li>
              <li>从基础库 2.0.9 开始，对象类型的属性和 data 字段中可以包含函数类型的子字段</li>
              <li>自小程序基础库版本 2.2.3 起，组件的的生命周期也可以在 lifetimes 字段内进行声明</li>
              <li><code>resize</code>(组件所在的页面尺寸变化，2.4.0开始支持)</li>
              <li>抽象节点自小程序基础库版本 1.9.6 开始支持</li>
              <li>自定义组件扩展从小程序基础库版本 2.2.3 开始支持。</li>
              <li>小程序从基础库版本 2.2.1 、开发者工具从 1.02.1808300 开始支持使用 npm 安装第三方包</li>
            </ul>
            <h1 id="导读"><a href="#导读" class="headerlink" title="导读"></a>导读</h1>
            <p>首先看几个学习地址，用于方便理解小程序组件的能力。</p>
            <p><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/">自定义组件</a><br><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html">组件模板和样式</a><br><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html">Component构造器</a></p>
            <p>** 组件npm开发 **<br><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/trdparty.html">开发第三方自定义组件</a><br><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/miniprogram-cli">命令行工具</a><br><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/miniprogram-custom-component">小程序自定义组件脚手架</a><br><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html">npm支持</a></p>
            <h1 id="自定义组件功能"><a href="#自定义组件功能" class="headerlink" title="自定义组件功能"></a>自定义组件功能</h1>
            <h2 id="组件模板"><a href="#组件模板" class="headerlink" title="组件模板"></a>组件模板</h2>
            <p>写法与页面模板相同</p>
            <h3 id="模板数据绑定"><a href="#模板数据绑定" class="headerlink" title="模板数据绑定"></a>模板数据绑定</h3>
            <p>向子组件的属性传递动态数据</p>
            <figure class="highlight html">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">&lt;!-- 引用组件的页面模板 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">component-tag-name</span> <span class="attr">prop-a</span>=<span class="string">&quot;&#123;&#123;dataFieldA&#125;&#125;&quot;</span> <span class="attr">prop-b</span>=<span class="string">&quot;&#123;&#123;dataFieldB&#125;&#125;&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 这部分内容将被放置在组件 &lt;slot&gt; 的位置上 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span>&gt;</span>这里是插入到组件slot中的内容<span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">component-tag-name</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>** 注意 **</p>
            <ul>
              <li>自基础库版本 2.0.9 开始，还可以在数据中包含函数</li>
            </ul>
            <h3 id="组件的slot"><a href="#组件的slot" class="headerlink" title="组件的slot"></a>组件的slot</h3>
            <p>在组件模板中可以提供一个 <code>&lt;slot&gt;</code> 节点，用于承载组件引用时提供的子节点。</p>
            <p>支持默认slot和具名slot</p>
            <p>** 注意 **</p>
            <ul>
              <li>slot是view组件的话，查看文档结构，slot的代码和组件代码是同级的</li>
            </ul>
            <h2 id="组件样式"><a href="#组件样式" class="headerlink" title="组件样式"></a>组件样式</h2>
            <h3 id="不支持的写法"><a href="#不支持的写法" class="headerlink" title="不支持的写法"></a>不支持的写法</h3>
            <figure class="highlight css">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="selector-id">#a</span> &#123; &#125; <span class="comment">/* ID选择器在组件中不能使用 */</span></span><br><span class="line"><span class="selector-attr">[a]</span> &#123; &#125; <span class="comment">/* 属性选择器在组件中不能使用 */</span></span><br><span class="line"><span class="selector-tag">button</span> &#123; &#125; <span class="comment">/* 标签名选择器在组件中不能使用 */</span></span><br><span class="line"><span class="selector-class">.a</span> &gt; <span class="selector-class">.b</span> &#123; &#125; <span class="comment">/* 除非 .a 是 view 组件节点，否则不一定会生效 */</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <h3 id="外部样式类"><a href="#外部样式类" class="headerlink" title="外部样式类"></a>外部样式类</h3>
            <p>在 <code>Component</code> 中用 <code>externalClasses</code> 定义段定义若干个外部样式类。这个特性从小程序基础库版本 1.9.90 开始支持</p>
            <p>注意：在同一个节点上使用普通样式类和外部样式类时，两个类的优先级是未定义的，因此最好避免这种情况。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">/* 组件 custom-component.js */</span></span><br><span class="line">Component(&#123;</span><br><span class="line">  externalClasses: [<span class="string">&#x27;my-class&#x27;</span>]</span><br><span class="line">&#125;)</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <figure class="highlight html">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">&lt;!-- 组件 custom-component.wxml --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">custom-component</span> <span class="attr">class</span>=<span class="string">&quot;my-class&quot;</span>&gt;</span>这段文本的颜色由组件外的 class 决定<span class="tag">&lt;/<span class="name">custom-component</span>&gt;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h3 id="全局样式类"><a href="#全局样式类" class="headerlink" title="全局样式类"></a>全局样式类</h3>
            <p>如果希望组件外样式类能够完全影响组件内部，可以将组件构造器中的options.addGlobalClass字段置为true。这个特性从小程序基础库版本 2.2.3 开始支持.</p>
            <h2 id="组件构造器"><a href="#组件构造器" class="headerlink" title="组件构造器"></a>组件构造器</h2>
            <ul>
              <li>在自定义组件的 js 文件中，需要使用 Component() 来注册组件，并提供组件的属性定义、内部数据和自定义方法。</li>
              <li>使用 this.data 可以获取内部数据和属性值，但不要直接修改它们，应使用 setData 修改。</li>
              <li>从基础库 2.0.9 开始，对象类型的属性和 data 字段中可以包含函数类型的子字段</li>
            </ul>
            <h2 id="组件事件"><a href="#组件事件" class="headerlink" title="组件事件"></a>组件事件</h2>
            <p>组件间的基本通信方式有以下几种。</p>
            <ul>
              <li>WXML 数据绑定：用于父组件向子组件的指定属性设置数据，仅能设置 JSON 兼容数据（自基础库版本 2.0.9 开始，还可以在数据中包含函数）。具体在 组件模板和样式 章节中介绍。</li>
              <li>事件：用于子组件向父组件传递数据，可以传递任意数据。</li>
              <li>如果以上两种方式不足以满足需要，父组件还可以通过 this.selectComponent 方法获取子组件实例对象，这样就可以直接访问组件的任意数据和方法。</li>
            </ul>
            <figure class="highlight html">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">&lt;!-- 监听事件 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 当自定义组件触发“myevent”事件时，调用“onMyEvent”方法 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">component-tag-name</span> <span class="attr">bindmyevent</span>=<span class="string">&quot;onMyEvent&quot;</span> /&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 或者可以写成 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">component-tag-name</span> <span class="attr">bind:myevent</span>=<span class="string">&quot;onMyEvent&quot;</span> /&gt;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 触发事件</span></span><br><span class="line">onTap: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> myEventDetail = &#123;&#125; <span class="comment">// detail对象，提供给事件监听函数</span></span><br><span class="line">  <span class="keyword">var</span> myEventOption = &#123;&#125; <span class="comment">// 触发事件的选项</span></span><br><span class="line">  <span class="built_in">this</span>.triggerEvent(<span class="string">&#x27;myevent&#x27;</span>, myEventDetail, myEventOption)</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>** 在写自定义form组件事件时，发送事件的数据detail对象使用<code>&#123;value: &#39;&#39;&#125;</code>格式，便于同监听原生form input事件时使用同一个input监听事件 **</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 事件监听方法</span></span><br><span class="line"><span class="function"><span class="title">inputHandler</span>(<span class="params">e</span>)</span> &#123;</span><br><span class="line">  <span class="built_in">this</span>.setData(&#123;</span><br><span class="line">    [e.currentTarget.dataset.key]: e.detail.value,</span><br><span class="line">  &#125;);</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 自定义input组件发送事件</span></span><br><span class="line">inputHandler: <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.triggerEvent(<span class="string">&#x27;myinput&#x27;</span>, &#123; <span class="attr">value</span>: <span class="number">123</span>&#125;);</span><br><span class="line">  &#125;,</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <figure class="highlight html">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">&lt;!-- 自定义组件绑定监听事件 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">yii-input</span> <span class="attr">value</span>=<span class="string">&quot;&#123;&#123;value&#125;&#125;&quot;</span> <span class="attr">data-key</span>=<span class="string">&quot;code&quot;</span> <span class="attr">bind:myinput</span>=<span class="string">&quot;inputHandler&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 原生组件绑定监听事件 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">&quot;&#123;&#123;value&#125;&#125;&quot;</span> <span class="attr">data-key</span>=<span class="string">&quot;code&quot;</span> <span class="attr">bindinput</span>=<span class="string">&quot;inputHandler&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">input</span>&gt;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h2 id="组件生命周期"><a href="#组件生命周期" class="headerlink" title="组件生命周期"></a>组件生命周期</h2>
            <p>最重要的生命周期是 <code>created</code>(组件实例刚刚被创建好时) <code>attached</code>(组件完全初始化完毕、进入页面节点树后) <code>detached</code>(在组件离开页面节点树后) ，包含一个组件实例生命流程的最主要时间点。</p>
            <p>生命周期方法可以直接定义在 Component 构造器的第一级参数中。</p>
            <p>自小程序基础库版本 2.2.3 起，组件的的生命周期也可以在 <code>lifetimes</code> 字段内进行声明（这是推荐的方式，其优先级最高）。如：<code>attached</code>、<code>detached</code></p>
            <p>在 <code>behaviors</code> 中也可以编写生命周期方法</p>
            <p>组件所在页面的生命周期在 <code>pageLifetimes</code> 定义段中定义<code>show</code>(组件所在的页面被展示) <code>hide</code>(组件所在的页面被隐藏) <code>resize</code>(组件所在的页面尺寸变化，2.4.0开始支持)</p>
            <h2 id="behaviors"><a href="#behaviors" class="headerlink" title="behaviors"></a>behaviors</h2>
            <p>用于组件间代码共享的特性，类似于一些编程语言中的“mixins”或“traits”。</p>
            <p>每个组件可以引用多个 behavior 。 behavior 也可以引用其他 behavior 。</p>
            <ul>
              <li>组件本身的属性或方法会覆盖 behavior 中的属性或方法，如果引用了多个 behavior ，在定义段中靠后 behavior 中的属性或方法会覆盖靠前的属性或方法；</li>
              <li>如果有同名的数据字段，如果数据是对象类型，会进行对象合并，如果是非对象类型则会进行相互覆盖；</li>
              <li>生命周期函数不会相互覆盖，而是在对应触发时机被逐个调用。</li>
            </ul>
            <p>内置 behaviors：<code>wx://form-field</code> <code>wx://component-export</code></p>
            <h2 id="组件间关系"><a href="#组件间关系" class="headerlink" title="组件间关系"></a>组件间关系</h2>
            <p>略</p>
            <h2 id="抽象节点"><a href="#抽象节点" class="headerlink" title="抽象节点"></a>抽象节点</h2>
            <p>这个特性自小程序基础库版本 1.9.6 开始支持。</p>
            <p>自定义组件模板中的一些节点，其对应的自定义组件不是由自定义组件本身确定的，而是自定义组件的调用者确定的。这时可以把这个节点声明为“抽象节点”。</p>
            <p>节点的 generic 引用 generic:xxx=”yyy” 中，值 yyy 只能是静态值，不能包含数据绑定。不能动态决定。</p>
            <h2 id="自定义组件扩展"><a href="#自定义组件扩展" class="headerlink" title="自定义组件扩展"></a>自定义组件扩展</h2>
            <p>从小程序基础库版本 2.2.3 开始支持。</p>
            <h2 id="使用自定义组件"><a href="#使用自定义组件" class="headerlink" title="使用自定义组件"></a>使用自定义组件</h2>
            <h3 id="声明局部组件"><a href="#声明局部组件" class="headerlink" title="声明局部组件"></a>声明局部组件</h3>
            <p>在<code>index.json</code>中对自定义组进行引用声明</p>
            <figure class="highlight json">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;usingComponents&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;component-tag-name&quot;</span>: <span class="string">&quot;path/to/the/custom/component&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>在<code>index.wxml</code>中使用</p>
            <figure class="highlight html">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 以下是对一个自定义组件的引用 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">component-tag-name</span> <span class="attr">inner-text</span>=<span class="string">&quot;Some text&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">component-tag-name</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h3 id="声明全局组件"><a href="#声明全局组件" class="headerlink" title="声明全局组件"></a>声明全局组件</h3>
            <p>在 app.json 中声明 usingComponents 字段，在此处声明的自定义组件视为全局自定义组件，在小程序内的页面或自定义组件中可以直接使用而无需再声明。</p>
            <p>** 注意 **</p>
            <ul>
              <li>自定义组件也是可以引用自定义组件的</li>
              <li>开发者工具 1.02.1810190 及以上版本支持声明全局组件</li>
            </ul>
            <blockquote>
              <p>这里不多说了，只记录关键信息，详细情况查看官方文档<a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/">自定义组件</a></p>
            </blockquote>
            <h1 id="开发第三方自定义组件"><a href="#开发第三方自定义组件" class="headerlink" title="开发第三方自定义组件"></a>开发第三方自定义组件</h1>
            <p>小程序从基础库版本 2.2.1 开始支持使用 npm 安装第三方包。</p>
            <h2 id="开发"><a href="#开发" class="headerlink" title="开发"></a>开发</h2>
            <h3 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h3>
            <p>可使用下载模板和命令行工具进行开发，推荐使用命令行开发，如果是已有项目则不需要。</p>
            <p>初始化之后需要安装依赖，安装依赖的时候可能会安装失败，在登录npm账号之后会正常安装npm包。如果不行试试<code>verdaccio</code>。</p>
            <figure class="highlight bash">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment"># 安装命令行工具</span></span><br><span class="line">npm install -g @wechat-miniprogram/miniprogram-cli</span><br><span class="line"><span class="comment"># 在指定目录初始化</span></span><br><span class="line">miniprogram init --<span class="built_in">type</span> custom-component</span><br><span class="line"><span class="comment"># npm 登录。可先跳过执行下面语句，不成功在回来执行</span></span><br><span class="line">npm login</span><br><span class="line"><span class="comment"># 安装依赖</span></span><br><span class="line">npm install</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/miniprogram-cli/blob/master/README.md">命令行工具用法</a></p>
            <p><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/miniprogram-custom-component/blob/master/README.md">项目模板用法</a></p>
            <h3 id="开发注意事项"><a href="#开发注意事项" class="headerlink" title="开发注意事项"></a>开发注意事项</h3>
            <p>为了方便开发执行命令<code>npm run watch</code>，默认会在包根目录下生成<code>miniprogram_dev</code>目录，<code>src</code>中的源代码会被构建并生成到<code>miniprogram_dev/components</code>目录下。<code>/tools/demo</code>下的内容也会复制到该目录下</p>
            <p>内置支持多入口构建，如若需要可自行调整 tools/config.js 配置文件的 entry 字段。<strong>无论是js模块还是自定义组件都要配置</strong></p>
            <p>执行测试<code>npm run test</code>、检测覆盖率<code>npm run coverage</code></p>
            <p>发布使用命令<code>npm run dist</code></p>
            <p><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/miniprogram-custom-component/blob/master/docs/test.md">测试工具文档</a></p>
            <p><strong>以下为推荐使用的目录结构，如果有必要开发者也可以自行做一些调整:</strong></p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">|--miniprogram_dev &#x2F;&#x2F; 开发环境构建目录</span><br><span class="line">|--miniprogram_dist &#x2F;&#x2F; 生产环境构建目录</span><br><span class="line">|--src &#x2F;&#x2F; 源码</span><br><span class="line">|   |--common &#x2F;&#x2F; 通用 js 模块</span><br><span class="line">|   |--components &#x2F;&#x2F; 通用自定义组件</span><br><span class="line">|   |--images &#x2F;&#x2F; 图片资源</span><br><span class="line">|   |--wxml &#x2F;&#x2F; 通用 wxml 模版资源</span><br><span class="line">|   |--wxs &#x2F;&#x2F; 通用 wxs 资源</span><br><span class="line">|   |--wxss &#x2F;&#x2F; 通用 wxss 资源</span><br><span class="line">|   |</span><br><span class="line">|   |--xxx.js&#x2F;xxx.wxml&#x2F;xxx.json&#x2F;xxx.wxss &#x2F;&#x2F; 暴露的 js 模块&#x2F;自定义组件入口文件</span><br><span class="line">|</span><br><span class="line">|--test &#x2F;&#x2F; 测试用例</span><br><span class="line">|--tools &#x2F;&#x2F; 构建相关代码</span><br><span class="line">|   |--demo &#x2F;&#x2F; demo 小程序目录，开发环境下会被拷贝生成到 miniprogram_dev 目录中</span><br><span class="line">|   |--test &#x2F;&#x2F; 测试工具相关目录</span><br><span class="line">|   |--config.js &#x2F;&#x2F; 构建相关配置文件</span><br><span class="line">|</span><br><span class="line">|--gulpfile.js</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>新加图片之后要重启服务</p>
            <h3 id="编写文档"><a href="#编写文档" class="headerlink" title="编写文档"></a>编写文档</h3>
            <p>包括但不限于版本说明、使用效果、使用方法、详细说明</p>
            <h2 id="发布"><a href="#发布" class="headerlink" title="发布"></a>发布</h2>
            <h3 id="发布小程序-npm-包的约束"><a href="#发布小程序-npm-包的约束" class="headerlink" title="发布小程序 npm 包的约束"></a>发布小程序 npm 包的约束</h3>
            <p>这里要发布的 npm 包是特指专为小程序定制的 npm 包（简称小程序 npm 包）。</p>
            <ol>
              <li>在 package.json 文件中新增一个 miniprogram 字段来指定构建文件生成目录（默认为 miniprogram_dist 目录）。</li>
              <li>小程序 npm 包里只有构建文件生成目录会被算入小程序包的占用空间。</li>
            </ol>
            <p>等</p>
            <h3 id="发布其他-npm-包的约束"><a href="#发布其他-npm-包的约束" class="headerlink" title="发布其他 npm 包的约束"></a>发布其他 npm 包的约束</h3>
            <ol>
              <li>只支持纯 js 包，不支持自定义组件。</li>
              <li>必须有入口文件，即需要保证 package.json 中的 main 字段是指向一个正确的入口，如果 package.json 中没有 main 字段，则以 npm 包根目录下的 index.js 作为入口文件。</li>
              <li>测试、构建相关的依赖请放入 devDependencies 字段中避免被一起打包到小程序包中，这一点和小程序 npm 包的要求相同。</li>
              <li>不支持依赖 c++ addon，不支持依赖 nodejs 的内置库：</li>
              <li>小程序环境比较特殊，一些全局变量（如 window 对象）和构造器（如 Function 构造器）是无法使用的。</li>
            </ol>
            <h3 id="发布流程"><a href="#发布流程" class="headerlink" title="发布流程"></a>发布流程</h3>
            <ol>
              <li>如果还没有 npm 帐号，可以到 npm 官网注册一个 npm 帐号。</li>
              <li>在本地登录 npm 帐号，在本地执行：<code>npm adduser</code>或者<code>npm login</code></li>
              <li>在已完成编写的 npm 包根目录下执行<code>npm publish</code></li>
            </ol>
            <h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2>
            <p><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html">npm支持</a></p>
            <ol>
              <li>
                <p>在小程序中执行命令安装 npm 包：</p>
                <figure class="highlight plain">
                  <table>
                    <tr>
                      <td class="gutter">
                        <pre><span class="line">1</span><br></pre>
                      </td>
                      <td class="code">
                        <pre><span class="line">npm install --save packageName</span><br></pre>
                      </td>
                    </tr>
                  </table>
                </figure>
              </li>
              <li>
                <p>点击开发者工具中的菜单栏：工具 –&gt; 构建 npm<br><img src="" data-original="/images/wechatapp/02.png" alt="02"></p>
              </li>
              <li>
                <p>勾选“使用 npm 模块”选项：<br><img src="" data-original="/images/wechatapp/03.png" alt="03"></p>
              </li>
              <li>
                <p>构建完成后即可使用 npm 包。</p>
              </li>
            </ol>
            <p>js 中引入 npm 包：</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">const package &#x3D; require(&#39;packageName&#39;)</span><br><span class="line">const packageOther &#x3D; require(&#39;packageName&#x2F;other&#39;)</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>使用 npm 包中的自定义组件：</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&#123;</span><br><span class="line">  &quot;usingComponents&quot;: &#123;</span><br><span class="line">    &quot;package&quot;: &quot;packageName&quot;,</span><br><span class="line">    &quot;package-other&quot;: &quot;packageName&#x2F;other&quot;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>思考：这里有一个<code>/other</code>，为什么是这样的，难道因为默认找index组件？如果使用其他组件就要这样使用？</p>
            <h2 id="官方自定义组件"><a href="#官方自定义组件" class="headerlink" title="官方自定义组件"></a>官方自定义组件</h2>
            <p><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/slide-view">slide-view</a><br><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/navigation-bar">navigation-bar</a><br><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/recycle-view">recycle-view</a></p>
            <h2 id="官方扩展"><a href="#官方扩展" class="headerlink" title="官方扩展"></a>官方扩展</h2>
            <p><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/computed">computed</a><br><a target="_blank" rel="noopener" href="https://github.com/wechat-miniprogram/sm-crypto">sm-crypto</a></p>

          </div>





          <footer class="post-footer">
            <div class="post-tags">
              <a href="/tags/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" rel="tag"># 微信小程序</a>
              <a href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6/" rel="tag"># 小程序自定义组件</a>
            </div>



            <div class="post-nav">
              <div class="post-nav-item">
                <a href="/blob/2018-05-29-git-code-merge-and-rebase-selection/" rel="prev" title="Git-代码合并Merge与Rebase的选择">
                  <i class="fa fa-chevron-left"></i> Git-代码合并Merge与Rebase的选择
                </a>
              </div>
              <div class="post-nav-item">
                <a href="/blob/2018-11-16-development-collection-of-wechat-small-procedure-plug-ins/" rel="next" title="微信小程插件开发合集">
                  微信小程插件开发合集 <i class="fa fa-chevron-right"></i>
                </a>
              </div>
            </div>
          </footer>
        </article>
      </div>







      <script>
        window.addEventListener('tabs:register', () => {
          let {
            activeClass
          } = CONFIG.comments;
          if (CONFIG.comments.storage) {
            activeClass = localStorage.getItem('comments_active') || activeClass;
          }
          if (activeClass) {
            const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
            if (activeTab) {
              activeTab.click();
            }
          }
        });
        if (CONFIG.comments.storage) {
          window.addEventListener('tabs:click', event => {
            if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
            const commentClass = event.target.classList[1];
            localStorage.setItem('comments_active', commentClass);
          });
        }
      </script>
    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


      <div class="copyright">
        &copy;
        <span itemprop="copyrightYear">2021</span>
        <span class="with-love">
          <i class="fa fa-heart"></i>
        </span>
        <span class="author" itemprop="copyrightHolder">holidaypenguin</span>
      </div>
      <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
      </div>

    </div>
  </footer>


  <script size="300" alpha="0.1" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
  <script src="/js/love.js"></script>

  <script src="/js/development.js"></script>
  <script src="/js/utils.js"></script>
  <script src="/js/motion.js"></script>
  <script src="/js/schemes/muse.js"></script>
  <script src="/js/next-boot.js"></script>


  <script src="/js/local-search.js"></script>









  <script>
    (function() {
      function leancloudSelector(url) {
        url = encodeURI(url);
        return document.getElementById(url).querySelector('.leancloud-visitors-count');
      }

      function addCount(Counter) {
        const visitors = document.querySelector('.leancloud_visitors');
        const url = decodeURI(visitors.id);
        const title = visitors.dataset.flagTitle;

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            if (results.length > 0) {
              const counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
              Counter('put', '/classes/Counter/' + counter.objectId, {
                  time: {
                    '__op': 'Increment',
                    'amount': 1
                  }
                })
                .catch(error => {
                  console.error('Failed to save visitor count', error);
                });
            } else {
              Counter('post', '/classes/Counter', {
                  title,
                  url,
                  time: 1
                })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      function showTime(Counter) {
        const visitors = document.querySelectorAll('.leancloud_visitors');
        const entries = [...visitors].map(element => {
          return decodeURI(element.id);
        });

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url: {
              '$in': entries
            }
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            for (let url of entries) {
              const target = results.find(item => item.url === url);
              leancloudSelector(url).innerText = target ? target.time : 0;
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      const {
        app_id,
        app_key,
        server_url
      } = {
        "enable": true,
        "app_id": "povuqNsSqFodlakVIwtEX5kb-gzGzoHsz",
        "app_key": "zXD40RDtDB3DMtpC89k0AK7g",
        "server_url": null,
        "security": false
      };

      function fetchData(api_server) {
        const Counter = (method, url, data) => {
          return fetch(`${api_server}/1.1${url}`, {
            method,
            headers: {
              'X-LC-Id': app_id,
              'X-LC-Key': app_key,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
          });
        };
        if (CONFIG.page.isPost) {
          if (CONFIG.hostname !== location.hostname) return;
          addCount(Counter);
        } else if (document.querySelectorAll('.post-title-link').length >= 1) {
          showTime(Counter);
        }
      }

      const api_server = app_id.slice(-9) === '-MdYXbMMI' ? `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com` : server_url;

      if (api_server) {
        fetchData(api_server);
      } else {
        fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
          .then(response => response.json())
          .then(({
            api_server
          }) => {
            fetchData('https://' + api_server);
          });
      }
    })();
  </script>



<script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(n){n.imageLazyLoadSetting.processImages=o;var e=n.imageLazyLoadSetting.isSPA,i=n.imageLazyLoadSetting.preloadRatio||1,r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function o(){e&&(r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")));for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(n.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];t=o,e=function(){r=r.filter(function(t){return o!==t})},n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,e&&e()},t.src!==i&&(n.src=i)}()}o(),n.addEventListener("scroll",function(){var t,e;t=o,e=n,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(e)},500)})}(this);</script></body>

</html>